window.onload = function(){
	loadAllMenu();		
	loadCartData(cart);	
}

//Initial variables

//菜单
var menu = new Menu();
//购物车
var cart = new Cart();
//右边购物车
var isSideBarHide = true;

// Hide side bar
function clicksidebar(){		
	var side = document.getElementById("side");
	if (isSideBarHide) {
		side.style.right="0px";
		isSideBarHide = false;
	}else{
		side.style.right="-300px";
		isSideBarHide = true;
	}	
}

function showsidebar(){
	side.style.right="0px";
	isSideBarHide = false;
}

// Show menu

function loadAllMenu(){
	var d = new dquery();
	d.getJSON("LoadAllMenu",function(result){
		menu.add(result);
		createMenu();
	})
}



function createMenu(){
	var content = document.getElementById('content');
	var dmenu = document.getElementById('dmenu');
	if(dmenu){
		content.removeChild(dmenu);
	}
	loadMenu(content);
}


function loadMenu(content){
	var tb = document.createElement("div");
	tb.setAttribute('id','dmenu');
	var index = 0;
	for(var i = 0; i < 8; i ++){
		var td = document.createElement("div");
		td.setAttribute('class','item');
		var icon = document.createElement("img");
		var name = document.createElement("p");
		name.setAttribute('class','name');
		var price = document.createElement("p");
		price.setAttribute('class','price');
		var slt = document.createElement("button");

		icon.setAttribute("src",menu.dishs[index].icon);

		var menuname = menu.dishs[index].getName();
		name.innerText=menuname;

		var	menuprice = menu.dishs[index].getPrice();
		price.innerText="Price: $"+menuprice;			

		slt.onclick = buy;
		slt.innerText="Buy";		
		slt.setAttribute('id',menu.dishs[index].getId());
		td.appendChild(icon);
		td.appendChild(name);
		td.appendChild(price);
		td.appendChild(slt);
		tb.appendChild(td);
		index++;

	}
	content.appendChild(tb);

}

function buy(){
	var id = this.getAttribute('id');
	cart.add(menu.getDishById(id));
	saveCartData(cart);
	cartTable(cart);
	showsidebar();
}

// Show cart

function checkout(){
	window.location.href="shopping.jsp";  
}

function loadCartData(cart){
	var d = new dquery();
	d.get("LoadCart", function(result){
		if(result == ""){
			cleanTable();
		}else{
			cart.debox(result);
			cartTable(cart);
		}		
	});
}

function saveCartData(cart){
	var d = new dquery();
	d.post("SaveCart", "cart="+cart.inbox(), function(result){
		var s = JSON.parse(result);
		if(s.status != 1){
			window.location.href="login.jsp";
		}
	});
}

function cleanTable(){
	var dc = document.getElementById("cartlist");
	var cmenu = document.getElementById('cmenu');
	if(cmenu){
		dc.removeChild(cmenu);
	}
}

function cartTable(cart){
	var dc = document.getElementById("cartlist");
	var cmenu = document.getElementById('cmenu');
	if(cmenu){
		dc.removeChild(cmenu);
	}
	var tb = document.createElement("div");
	tb.setAttribute('id','cmenu');
	for(var i=0;i<cart.size();i++){
		if(cart.cartList[i].num != 0){
			var tr = document.createElement("div");
			tr.setAttribute('id',i);
			tr.setAttribute('class','row');
			var tit = document.createElement("p");
			tit.setAttribute('class','name');
			var val = document.createElement("p");
			val.setAttribute('class','price');
			var addbtn = document.createElement("button");
			addbtn.setAttribute('class','rbtn');
			var delbtn = document.createElement("button");
			delbtn.setAttribute('class','rbtn');
			var num = document.createElement("input");

			tit.innerText=cart.cartList[i].getDish().getName();
			val.innerText=cart.cartList[i].getDish().getPrice();
			addbtn.innerText="+";
			addbtn.onclick=addNum;
			delbtn.onclick=delNum;
			delbtn.innerText="-";
			num.setAttribute('value',cart.cartList[i].num);

			tr.appendChild(tit);
			tr.appendChild(delbtn);
			tr.appendChild(num);
			tr.appendChild(addbtn);
			tr.appendChild(val);
			tb.appendChild(tr);	
		}		
	}

	var tr = document.createElement("div");
	tr.setAttribute("id","total");
	var total = document.createElement("p");
	total.innerText = "Total : $ "+cart.calculate();
	var pbtn = document.createElement("button");
	pbtn.innerText = "CheckOut";
	pbtn.onclick=checkout;
	tr.appendChild(total);
	tr.appendChild(pbtn);
	tb.appendChild(tr);
	dc.appendChild(tb);	
}

function addNum(){
	var tr = this.parentNode;
	var id = tr.getAttribute("id");
	cart.cartList[id].up();
	saveCartData(cart);
	cartTable(cart);
}

function delNum(){
	var tr = this.parentNode;
	var id = tr.getAttribute("id");	
	cart.cartList[id].down();
	if(cart.cartList[id].num == 0){
		var p = tr.parentNode;
		p.removeChild(tr);
	}	
	saveCartData(cart);
	cartTable(cart);
}